package cn.wei.reallyEnjoy.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import cn.wei.reallyEnjoy.R;
import cn.wei.reallyEnjoy.app.RecordDetailActivity;
import cn.wei.reallyEnjoy.home.bean.BannerBean;
import cn.wei.reallyEnjoy.home.bean.DiaryBean;
import cn.wei.reallyEnjoy.home.bean.IndexBean;
import cn.wei.reallyEnjoy.home.bean.RecordBean;
import cn.wei.reallyEnjoy.utils.Constants;

public class HomeFragmentAdapter extends RecyclerView.Adapter {
    /**
     * 轮播图类型
     */
    private static final int  BANNER = 0;

    /**
     * 推荐
     */
    private static final int  RECOMMEND = 1;
    /**
     * 初始化布局
     */
    private  LayoutInflater mLayoutInflater;
    private  Context mContext;
    private   IndexBean indexBean;

    /**
     * 当前类型
     */
    private int currentType = BANNER;

    public HomeFragmentAdapter(Context mContext,IndexBean indexBean) {
        this.mContext = mContext;
        this.indexBean = indexBean;
        mLayoutInflater = LayoutInflater.from(mContext);

    }

    /**
     * 相当 于getView
     * 创建 ViewHolder 部分代码
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==BANNER){
             //返回BannerViewHolder
           return new BannerViewHolder(mContext,mLayoutInflater.inflate(R.layout.banner_viewpager,null));
        }
        else if (viewType==RECOMMEND){
            return new RecommendViewHolder(mContext,mLayoutInflater.inflate(R.layout.recommend_item,null));
        }

        return null;
    }

    /**
     * 相当于getView中的绑定数据模块
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position)==BANNER){
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(indexBean.getBannerList());
        }else if (getItemViewType(position)==RECOMMEND){
            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
            recommendViewHolder.setData(indexBean.getDiaryIndexList());
        }
    }

    /**
     * 得到的类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position){
            case BANNER:
                currentType = BANNER;
                break;
            case RECOMMEND:
                currentType = RECOMMEND;
                break;
        }
        return currentType;
    }

    /**
     * 总共有多少个item
     * @return
     */
    @Override
    public int getItemCount() {
        //开发过程中要从1开始1--》2===》3
        return 2;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder{
        private Context context;
        private Banner banner;

        public BannerViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.context = mContext;
            this.banner = itemView.findViewById(R.id.banner);
        }

        public void setData(List<BannerBean> bannerBeanList) {
            //设置banner数据
            List<String> imagesUrl = new ArrayList<>();
//            得到图片集合
            for (int i = 0; i < bannerBeanList.size(); i++) {
                imagesUrl.add(bannerBeanList.get(i).getBannerUrl());
            }
            banner.setImages(imagesUrl);
            //样式
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
            //加载器
            banner.setImageLoader(new MyImageLoader());
            //动画效果
            banner.setBannerAnimation(Transformer.ZoomOutSlide);
            //banner无标题样式
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);

            //间隔时间
            banner.setDelayTime(4500);
            //是否为自动轮播
            banner.isAutoPlay(true);
            //图片小点显示所在位置
            banner.setIndicatorGravity(BannerConfig.CENTER);
            //启动轮播图。
            banner.start();
            //监听轮播图
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    Toast.makeText(mContext, "点击了" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
                }
            });

        }
    }
    class RecommendViewHolder extends RecyclerView.ViewHolder{
        private  Context mContext;

        private GridView gv_hot;
        private RecommendGridVIewAdapter adapter;

        public RecommendViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;

            gv_hot = itemView.findViewById(R.id.gv_hot);




        }

        public void setData(List<DiaryBean> diaryBeanList) {

            //设置适配器
            adapter = new RecommendGridVIewAdapter(mContext,diaryBeanList);
            gv_hot.setAdapter(adapter);

            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //热门推荐记录对象
                    //TODO
                    DiaryBean diaryBean = diaryBeanList.get(position);
                    RecordBean recordBean = new RecordBean();

                    //记录对象
                    //TODO
                    recordBean.setRecord_id(diaryBean.getId());
                    recordBean.setImg(diaryBean.getContentImg());
                    recordBean.setContent(diaryBean.getContent());
                    recordBean.setTitle(diaryBean.getTitle());
                    recordBean.setUname(diaryBean.getUname());
                    recordBean.setUheadImg(diaryBean.getUheadImg());
                    recordBean.setPrefer(diaryBean.getPrefer());
                    recordBean.setCreateTime(diaryBean.getCreateTime());
                    startRecordDetailActivity(recordBean);
                }
            });
        }
    }

    private void startRecordDetailActivity(RecordBean recordBean) {
        Intent intent = new Intent(mContext, RecordDetailActivity.class);
        intent.putExtra("recordBean",recordBean);
        mContext.startActivity(intent);
    }

}
